﻿Public Class Form1

   
    Dim Latitude
    Dim Longitude
    Public s1
    Public s2
    Public s3
    Public s4
    Public s5
    Public s6
    Public s7
    Public s8
    Public s9
    Public s10
    Public s11
    Public s12
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

        Try
            ListBox1.SelectedIndex = ListBox1.Items.Count - 1


            ListBox2.SelectedIndex = ListBox2.Items.Count - 1
            ListBox3.SelectedIndex = ListBox3.Items.Count - 1


        Catch
            ListBox1.Items.Add("Fehler in Stream")
        End Try
1:


        Try
            If Not SerialPort1.IsOpen Then
                SerialPort1.PortName = ComboBox1.Text
                SerialPort1.Open()
                Label1.Text = "Connected"
                Label1.ForeColor = Color.Green
                WorkbenchToolStripMenuItem.ForeColor = Color.Green
                ToolsToolStripMenuItem.ForeColor = Color.Green
            End If
        Catch ex As Exception

            Timer1.Stop()

            Exit Sub

        End Try


        If SerialPort1.IsOpen Then
            Dim data As String = SerialPort1.ReadExisting()
            Dim strArr() As String = data.Split("$")
            Dim i As Integer = 0
            If strArr.Length > 1 Then
                Dim strTemp As String


                For i = 0 To strArr.Length
                    Try
                        strTemp = strArr(i)
                    Catch

                        GoTo 1
                    End Try

                    Dim lineArr() As String = strTemp.Split(",")


                    If (lineArr(0).Contains("GPRMC")) Then
                        Try
                            Dim dLat As Double = Convert.ToDouble(lineArr(3).Replace(".", ","))
                            dLat = dLat / 100
                            Dim lat() As String = dLat.ToString().Split(",")
                            Latitude = lat(0).ToString() + "." + ((Convert.ToInt32(lat(1)) / 60)).ToString("####")


                            Dim dLong As Double = Convert.ToDouble(lineArr(5).Replace(".", ","))
                            dLong = dLong / 100
                            Dim lon() As String = dLong.ToString().Split(",")
                            Longitude = lon(0).ToString() + "." + ((Convert.ToInt32(lon(1)) / 60)).ToString("####")

                            ListBox1.Items.Add(strTemp)
                            ListBox2.Items.Add("Latitude(" & lineArr(4) & "): " & Latitude & "  Longitude(" & lineArr(6) & "): " & Longitude)
                            Call ko.tick(lineArr(6), lineArr(4), Longitude, Latitude)
                            Call Map.tick(lineArr(6) & "+" & Longitude, lineArr(4) & "+" & Latitude)
                            Call map2.tick(lineArr(6) & "+" & Longitude, lineArr(4) & "+" & Latitude)
                            Call GPX.tick(Latitude, Longitude, up)
                        Catch
                        End Try
                        Try


                            Dim time = lineArr(1)
                            time = Microsoft.VisualBasic.Left(time, 6)

                            Dim h = Microsoft.VisualBasic.Left(time, 2)


                            Dim min = Microsoft.VisualBasic.Left(time, 4)
                            min = Microsoft.VisualBasic.Right(min, 2)


                            Dim sec = Microsoft.VisualBasic.Right(time, 2)


                            Dim date1 = lineArr(9)
                            date1 = Microsoft.VisualBasic.Left(date1, 6)

                            Dim day = Microsoft.VisualBasic.Left(date1, 2)


                            Dim mon = Microsoft.VisualBasic.Left(date1, 4)
                            mon = Microsoft.VisualBasic.Right(mon, 2)


                            Dim j = Microsoft.VisualBasic.Right(date1, 2)


                            If h = "" Then
                                ListBox3.Items.Add("Fehler in Hour")
                            Else
                                If min = "" Then
                                    ListBox3.Items.Add("Fehler in Minutes")
                                Else
                                    If sec = "" Then
                                        ListBox3.Items.Add("Fehler in Seconds")
                                    Else
                                        ListBox3.Items.Add(h & ":" & min & ":" & sec)
                                        Call Uhr.tick(h, min, sec, day, mon, j)
                                        Call Timesync.tick(h, min, sec, day, mon, j)


                                    End If

                                End If

                            End If


                        Catch
                            ListBox3.Items.Add("Fehler in Stream")
                        End Try
                        Try
                            Dim k = lineArr(7).Replace(".", ",")
                            Call Temo.tick(k)

                        Catch
                            ListBox1.Items.Add("Fehler in Stream")
                            ListBox1.Items.Add("Fehler in Stream")
                        End Try

                        Try
                            Dim k1 = lineArr(8).Replace(".", ",")

                            Call compass.tick(k1)

                        Catch
                            ListBox1.Items.Add("Fehler in Stream")
                            ListBox1.Items.Add("Fehler in Stream")
                        End Try

                    End If


                    If (lineArr(0).Contains("GPGGA")) Then

                        Try
                            Dim qu = lineArr(6)

                            If qu = 0 Then
                                Label5.Text = "ungültig"
                                Label5.ForeColor = Color.Red
                            Else
                                If qu = 1 Then
                                    Label5.Text = "GPS"
                                    Label5.ForeColor = Color.Green
                                Else

                                    If qu = 2 Then
                                        Label5.Text = "DGPS"
                                        Label5.ForeColor = Color.Green
                                    Else

                                        If qu = 6 Then
                                            Label5.Text = "geschätzt"
                                            Label5.ForeColor = Color.OrangeRed
                                        Else
                                            Label5.Text = "ungültig"
                                            Label5.ForeColor = Color.Red

                                        End If
                                    End If

                                End If
                            End If

                        Catch

                            ListBox1.Items.Add("Fehler in Stream")
                        End Try

                        Try
                            Dim sd = lineArr(7)



                            If sd = 0 Then
                                Label4.Text = sd
                                Label4.ForeColor = Color.Red
                            Else
                                If sd < 2 Then
                                    Label4.Text = sd
                                    Label4.ForeColor = Color.OrangeRed
                                Else
                                    If sd > 2 Then
                                        Label4.Text = sd
                                        Label4.ForeColor = Color.Green
                                    Else

                                    End If

                                End If


                            End If
                        Catch
                            ListBox1.Items.Add("Fehler in Stream")
                        End Try
                        Try
                            Dim m = lineArr(9)
                            Dim m2 = lineArr(11)
                            Call Höhe.tick(m, m2)
                            up = m2








                            ListBox1.Items.Add(strTemp)


                        Catch
                            ListBox1.Items.Add("Fehler in Stream")
                        End Try
                    End If

                    If (lineArr(0).Contains("GPGSA")) Then

                        Try
                            Dim qu = lineArr(2)

                           
                                If qu = 1 Then
                                Label6.Text = "no FIX"
                                Label6.ForeColor = Color.Red
                                Else

                                    If qu = 2 Then
                                    Label6.Text = "2D-FIX"
                                    Label6.ForeColor = Color.OrangeRed
                                    Else

                                    If qu = 3 Then
                                        Label6.Text = "3D-FIX"
                                        Label6.ForeColor = Color.Green
                                    Else
                                        Label6.Text = "ungültig"
                                        Label6.ForeColor = Color.Red

                                    End If
                                    End If

                                End If


                        Catch
                            ListBox1.Items.Add("Fehler in Stream")
                        End Try

                        Try
                            s1 = lineArr(3)
                            s2 = lineArr(4)
                            s3 = lineArr(5)
                            s4 = lineArr(6)
                            s5 = lineArr(7)
                            s6 = lineArr(8)
                            s7 = lineArr(9)
                            s8 = lineArr(10)
                            s9 = lineArr(11)
                            s10 = lineArr(12)
                            s11 = lineArr(13)
                            s12 = lineArr(14)
                            Call PRN_Nummern.tick()
                        Catch
                            ListBox1.Items.Add("Fehler in Stream")
                        End Try



                        ListBox1.Items.Add(strTemp)

                    End If








                Next
            End If
        Else


        End If

    End Sub

   
    Dim up
    Private Sub Form1_FormClosed(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles MyBase.FormClosed
        Timer1.Stop()
        SerialPort1.Close()
        Label1.Text = "Disconnected"
        Label1.ForeColor = Color.Red
    End Sub

    Private Sub CloseToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CloseToolStripMenuItem.Click
        Timer1.Stop()
        SerialPort1.Close()
        Label1.Text = "Disconnected"
        Label1.ForeColor = Color.Red
        WorkbenchToolStripMenuItem.ForeColor = Color.Red
        ToolsToolStripMenuItem.ForeColor = Color.Red
    End Sub

    Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click
        Label1.Text = "Waiting for connection ..."
        Timer1.Start()
    End Sub

    Private Sub ClearLogToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClearLogToolStripMenuItem.Click
        ListBox1.Items.Clear()
        ListBox2.Items.Clear()
        ListBox3.Items.Clear()
    End Sub

   
  
    Private Sub UhrToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UhrToolStripMenuItem.Click
        Uhr.Show()
    End Sub

    Private Sub KoordinatenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles KoordinatenToolStripMenuItem.Click
        ko.show()
    End Sub

    Private Sub MapToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MapToolStripMenuItem.Click
        Map.show()
    End Sub

    Private Sub CompassToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CompassToolStripMenuItem.Click
        compass.Show()
    End Sub

    
    Private Sub HöheToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles HöheToolStripMenuItem.Click
        Höhe.Show()
    End Sub

    Private Sub TemoToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TemoToolStripMenuItem.Click
        Temo.Show()
    End Sub

    Private Sub PRNNumbersToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PRNNumbersToolStripMenuItem.Click
        PRN_Nummern.Show()
    End Sub

    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.Click
        Call Edit.tick(ListBox1.Text)
        Edit.Show()
    End Sub

    Private Sub ListBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox2.Click
        Call Edit.tick(ListBox2.Text)
        Edit.Show()
    End Sub

    Private Sub ListBox3_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox3.Click
        Call Edit.tick(ListBox3.Text)
        Edit.Show()
    End Sub

    Private Sub SystemZeitSyncToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SystemZeitSyncToolStripMenuItem.Click
        Timesync.Show()
    End Sub

   
    Private Sub NMEARecordToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NMEARecordToolStripMenuItem.Click
        NMEA.Show()
    End Sub

    Private Sub GPXRecordToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GPXRecordToolStripMenuItem.Click
        GPX.Show()
    End Sub

    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        Application.Exit()
    End Sub

    Private Sub NMEARECGPXToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NMEARECGPXToolStripMenuItem.Click
        Ntg.Show()
    End Sub

    Private Sub InfoToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles InfoToolStripMenuItem.Click
        AboutBox1.Show()
    End Sub

    Private Sub RouteToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RouteToolStripMenuItem.Click
        map2.Show()
    End Sub
End Class
